package com.vcredit.creditcat.service.ext;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.enumtype.AuthItemEnum;
import com.vcredit.creditcat.enumtype.auth.SupplementIemCategoryEnum;
import com.vcredit.creditcat.model.AuthItem;
import com.vcredit.creditcat.model.ProcessSpecial;
import com.vcredit.creditcat.model.ProcessSpecialItem;
import com.vcredit.creditcat.model.ProductSupplementItem;
import com.vcredit.creditcat.service.AuthItemService;
import com.vcredit.creditcat.service.ProductSupplementItemService;
import com.vcredit.creditcat.service.speedloan.ProcessSpecialItemService;
import com.vcredit.creditcat.service.speedloan.ProcessSpecialService;

@Service
public class ProcessSpecialItemExtService {

	@Autowired
	private ProcessSpecialItemService processSpecialItemService;
	@Autowired
	private ProcessSpecialService processSpecialService;
	@Autowired
	private AuthItemService authItemService;
	@Autowired
	private ProductSupplementItemService productSupplementItemService;
	
	public List<ProcessSpecialItem> findByProductAndType(Long productId, Integer type) {
		List<ProcessSpecial> allProcessSpecial = processSpecialService.findAll();
		for (ProcessSpecial processSpecial : allProcessSpecial) {
			if(processSpecial.getProductId().longValue() == productId) {
				return this.findBySpecailIdAndType(processSpecial.getSpecialId(), type);
			}
		}
		return new ArrayList<ProcessSpecialItem>();
	}
	
	private  List<ProcessSpecialItem> findBySpecailIdAndType(Long specailId, Integer type) {
		List<ProcessSpecialItem> result = new ArrayList<ProcessSpecialItem>();
		List<ProcessSpecialItem> allProcessSpecialItem = processSpecialItemService.findAll();
		for (ProcessSpecialItem processSpecialItem : allProcessSpecialItem) {
			if(processSpecialItem.getSpecialId().longValue() == specailId) {
				if(null == type) {
					result.add(processSpecialItem);
				}
				
				else if(type == processSpecialItem.getItemType().intValue()) {
					result.add(processSpecialItem);
				}
			}
		}
		return result;
	}

	/**
	 * 根据产品查询手机认证的流程项
	 * @author zhaoqi01
	 * @param productId
	 * @return
	 */
	public ProcessSpecialItem findByProductForMobileItem(Long productId) {
		List<ProcessSpecialItem> processSpecialItems = this.findByProductAndType(productId, null);
		Map<Long, AuthItem> allAuthItem = authItemService.findAll();
		Map<Long, ProductSupplementItem> allSupplementItem = productSupplementItemService.findAllForMap();
		for (ProcessSpecialItem processSpecialItem : processSpecialItems) {
			if(processSpecialItem.getItemType() == 0) { // 原始认证项
				// 根据itemId查询原始认证项配置
				AuthItem authItem = allAuthItem.get(processSpecialItem.getItemId());
				if(authItem.getAuthCode().intValue() == AuthItemEnum.MOBILE.getCode()) {
					return processSpecialItem;
				}
			} else if(processSpecialItem.getItemType() == 1) { // 补充认证项
				// 根据itemId查询补充认证项配置
				ProductSupplementItem supplementItem = allSupplementItem.get(processSpecialItem.getItemId());
				if(supplementItem.getSupplementCategoryId().intValue() == SupplementIemCategoryEnum.MOBILE.getCategory()) {
					return processSpecialItem;
				}
			}
		}
		return null;
	}
}
